<div class="card">
    <div class="content">
        <h5 class="">基础规则设定</h5>
        <p class="category">最低中奖条件，当抓取 <input type="number" min="1" ng-value="activity.basic.min_num" name="rule[basic][min_num]" style="width: 50px;">
            个红包后才可按照奖品中奖概率中奖。</p>
        <p class="category">活动持续时间<input type="number" min="0" string-to-number="" ng-model="activity.previewData.time" ng-value="activity.basic.time" name="rule[basic][time]" style="width:50px;"> 秒</p>
    </div>
</div>
<div class="card">
    <div class="content table-responsive">
        <h5 class="">奖项设置</h5>
        <table class="table table-hover table-striped">
            <thead>
            <tr>
                <th>图片</th>
                <th>名称</th>
                <th>中奖限制</th>
                <th>库存限制</th>
                <th>红包限额</th>
                <th>中奖概率</th>
                <th class="text-right">操作</th>
            </tr>
            </thead>
            <tbody class="prizes-table">
            <tr ng-repeat="pri in activity.prizes" data-index="@{{$index + 1}}">

                <td class="td-image"><input type="hidden" name="prizes[@{{$index + 1}}][id]" value="@{{pri.id}}"><img
                            ng-src="@{{pri.background_image}}" alt=""></td>
                <td class="prize_name">@{{pri.name}}</td>
                <td class="td-input"><input type="number" name="prizes[@{{$index + 1}}][winning_limit]"
                                            value="@{{pri.winning_limit}}" min="0" tooltip data-title="限制每一个用户最多能抽中该奖品多少次，填0表示不限制"></td>
                <td class="td-input"><input type="number" name="prizes[@{{$index + 1}}][stock_limit]" tooltip data-available_stock="@{{ pri.available_stock }}" data-title="最小可设置为0，最大不能超过@{{ pri.available_stock }}，如果为0，可能是因为其他活动使用该奖品，占用了库存（红包奖品为此次活动的总金额，微信红包每次只能发1-200元）"
                                            value="@{{pri.stock_limit}}" min="0" ></td>
                <td class="td-input"><input ng-if="pri.type == 'redpack'" type="number" name="prizes[@{{$index + 1}}][max_amount]" tooltip data-title="最小可设置为1，最大不能超过设置的库存限制"
                                            value="@{{pri.max_amount}}"  data-max-msg="最大值不能超过200"></td>
                <td class="td-input"><input type="number" name="prizes[@{{$index + 1}}][winning_probability]" tooltip data-title="中奖概率=奖品数/预估的参与人数*100%,最小可设置为0.最大可设置100% ,注:所有奖品概率总和不能大于100%"
                                            value="@{{pri.winning_probability}}" min="0">%
                </td>
                <td class="td-actions text-right"><a class="btn btn-info btn-simple btn-xs"
                                                     ng-click="removeAddedPrices(pri)" href="javascript:;">删除</a></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="footer">
        <a href="javascript:;" class="btn btn-fill btn-round btn-xs btn-warning" onclick="addPrizes()">添加奖品</a>
    </div>
</div>
<div class="card">
    {{--<div class="content specContent">--}}
        {{--<div>--}}
            {{--<h5 class="" style="float: left;margin-right: 10px;">--}}
                {{--特殊规则设置--}}
            {{--</h5>--}}
            {{--<div class="" bootstrap-switch data-ischecked="@{{activity.specs_isopen}}" style="margin-top: 5px;" data-on-label="<i class='fa fa-check'></i>"--}}
                 {{--data-off-label="<i class='fa fa-times'></i>" data-on-color="success">--}}
                {{--<input type="checkbox" name="rule[spec][isopen]"  value="1" >--}}
            {{--</div>--}}
            {{--<a href="javascript:;" style="margin-top: 10px;" ng-if="activity.specs_isopen == 1" id="addSpec" ng-click="addSpec()"--}}
               {{--class="pull-right text-danger specIsShow">+新增</a>--}}
        {{--</div>--}}
        {{--<div class="clearfix"></div>--}}
        {{--<div class="table-responsive specIsShow" id="specContentTable" ng-if="activity.specs_isopen == 1">--}}
            {{--<table class="table spec-table">--}}
                {{--<tbody>--}}
                {{--<tr class="spec-list" ng-repeat="spec in activity.specs" data-id="@{{spec.id}}">--}}
                    {{--<td >--}}
                        {{--<div class="form-group"><label class="control-label pull-left" for="">每个用户在</label>--}}
                            {{--<div class="pull-left" style="position: relative;"><input type="text" placeholder="开始时间" ng-value="spec.start"--}}
                                                                                      {{--required=""--}}
                                                                                      {{--name="rule[spec][list][@{{spec.id}}][start]"--}}
                                                                                      {{--data-msg="请输入开始时间"--}}
                                                                                      {{--class="form-control " date-time-picker>--}}
                            {{--</div>--}}
                            {{--<label class="control-label pull-left" for="">-</label>--}}
                            {{--<div class="pull-left" style="position: relative;"><input type="text" placeholder="结束时间" ng-value="spec.end"--}}
                                                                                      {{--required="" data-msg="请输入结束时间"--}}
                                                                                      {{--name="rule[spec][list][@{{spec.id}}][end]"--}}
                                                                                      {{--class="form-control " date-time-picker>--}}
                            {{--</div>--}}
                            {{--<label class="control-label pull-left" for="">100%抽中</label>--}}
                            {{--<div class="pull-left"><select name="rule[spec][list][@{{spec.id}}][prize]" string-to-number="" ng-model="spec.prize" class="form-control"--}}
                                                           {{--data-style="btn-default btn-block"--}}
                                                           {{--data-menu-style="dropdown-blue">--}}
                                    {{--<option ng-repeat="przi in $parent.activity.prizes" ng-selected="$parent.spec.prize ==przi.id" value="@{{przi.id}}">@{{przi.name}}</option>--}}
                                {{--</select></div>--}}
                            {{--<div class="pull-left spec-number-input"><input type="number" ng-value="spec.times"--}}
                                                                            {{--name="rule[spec][list][@{{spec.id}}][times]" rel="tooltip"--}}
                                                                            {{--title="" class="form-control"--}}
                                                                            {{--data-original-title="不能超过中奖限制"></div>--}}
                            {{--<label class="control-label pull-left" for="">次</label></div>--}}
                    {{--</td>--}}
                    {{--<td class="text-right">--}}
                        {{--<div class="form-group"><a class="control-label text-danger btn btn-simple" href="javascript:;"--}}
                                                   {{--ng-click="removeSpec(spec)">删除</a></div>--}}
                    {{--</td>--}}
                {{--</tr>--}}
                {{--</tbody>--}}
            {{--</table>--}}
        {{--</div>--}}

    {{--</div>--}}
</div>

<script>

    /**
     * 验证各个活动奖品设置中输入的表单数据
     */
    function valideRuleForm() {
        var form = $('#activityForm');
        if (form.find('input[name="rule[basic][min_num]"]').val() == '') {
            utils.showNotification('请输入最低抓取红包数才能触发中奖', 3);
            return false;
        }
        if (form.find('input[name="rule[basic][time]"]').val() == '') {
            utils.showNotification('请输入单场活动持续时间', 3);
            return false;
        }
        if (form.find('.prizes-table tr').length == 0) {
            utils.showNotification('请添加活动奖品', 3);
            return false;
        }
        var probability = 0,hasError = false;
        $('.prizes-table tr').each(function (i,v) {
            var index = $(v).data('index');
            var stock_limit = $('input[name="prizes['+index+'][stock_limit]"]').val();
            var prizename = $(v).find('.prize_name').text();
            var available_stock = $('input[name="prizes['+index+'][stock_limit]"]').attr('data-available_stock');
            if ($('input[name="prizes['+index+'][max_amount]"]').length >0){
                var max_amount = parseFloat($('input[name="prizes['+index+'][max_amount]"]').val());
                if (max_amount<1 ||max_amount>200 || max_amount>stock_limit){
                    utils.showNotification(prizename+'红包限额不能超过库存限制，并且只能设置在1-200范围', 3);
                    hasError = true;
                    return false;
                }
            }
            if (parseInt(stock_limit)>parseInt(available_stock)){
                utils.showNotification(prizename+'奖品的库存限制不能大于可用库存', 3);
                hasError = true;
                return false;
            }
            probability += parseFloat($('input[name="prizes['+index+'][winning_probability]"]').val());
        });
        if (hasError){
            return false;
        }
        if (probability >100){
            utils.showNotification('中奖概率不能大于100', 3);
            return false;
        }
        return true;
    }



    function addPrizes() {
        var addprize = utils.modal('添加奖品', function (ele) {
            var category = $('input[name="category"]').val();
            var html = '<table class="table" id="prizesTable"><thead><tr><th data-field="name">奖品名称</th><th data-field="prize_type.name" data-formatter="typeFormatter">类型</th><th data-formatter="expiredFormatter">有效期</th><th class="text-center" data-formatter="stockFormatter">可用/库存</th><th data-field="actions" class="td-actions text-center" data-events="operateEvents" data-formatter="operateFormatter">操作</th></tr></thead></table>';
            ele.find('.modal-body').html(html);
            var exclude = $('.prizes-table').attr('data-exclude') || '';
            var table_option = {
                searchAlign: 'left',
                queryParams: function (query) {
                    query['category'] = category;
                    query['exclude'] = exclude;
                    return query;
                },
                url: '/web/prize/queryPrizes'
            }
            default_table_option = $.extend(default_table_option, table_option);
            $('#prizesTable').bootstrapTable(default_table_option);
            $('#prizesTable').on('click', '.selectPrize', function () {
                addprize.modal('hide');
            })
        }, false, function (res) {
            if (res == 1) {
                addprize.modal('hide');
            }
        })

    }


    function typeFormatter(value, row) {
        return row.prize_type.name;
    }
    function expiredFormatter(value, row) {

        return (row.expired_start||'-') + '<br/>' + (row.expired_end||'-');
    }
    function stockFormatter(value, row) {
        var available_stock = row.available_stock ? row.available_stock : 0;
        var stock_number = row.stock_number ? row.stock_number : 0;
        return available_stock + '/' + stock_number;
    }
    function operateFormatter(value, row) {
        return '<a class="btn btn-warning btn-fill selectPrize">选取</a>';
    }

</script>